//
//  TenViewController.m
//  通讯录罚写10
//
//  Created by dllo on 15/11/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "TenViewController.h"
#import "Children.h"
#import "DetailViewController.h"
#import "AddViewController.h"
#import "ChineseToPinyin.h"

@interface TenViewController ()<UITableViewDataSource, UITableViewDelegate, AddViewControllerDelegate>

@property (nonatomic, retain) NSMutableDictionary *childDic;
@property (nonatomic, retain) UITableView *tableV;
@end

@implementation TenViewController

- (void)dealloc
{
    [_childDic release];
    [_tableV release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self getData];
    
    self.tableV = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    [_tableV release];
    
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(leftAction:)];
    
    
}
// 协议方法
- (void)addChildren:(Children *)child
{
    if (child != nil) {
        NSString *key = [[ChineseToPinyin pinyinFromChiniseString:child.name] substringToIndex:1];
        NSMutableArray *arr = nil;
        if ([self.childDic objectForKey:key] != nil) {
            arr = [self.childDic objectForKey:key];
            [arr addObject:child];
        } else {
            arr = [NSMutableArray array];
            [arr addObject:child];
            [self.childDic setObject:arr forKey:key];
        }
        [self.tableV reloadData];
    }
    
}
// 左键方法
- (void)leftAction:(UIBarButtonItem *)sender
{
    AddViewController *addVC = [[AddViewController alloc] init];
    [self.navigationController pushViewController:addVC animated:YES];
    [addVC release];
    addVC.delegate = self;
    
}

// 监测移动
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    if (sourceIndexPath.section != proposedDestinationIndexPath.section) {
        return sourceIndexPath;
    } else {
        return proposedDestinationIndexPath;
    }
    
    
}

//移动完成
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSString *key = [self.childDic.allKeys objectAtIndex:sourceIndexPath.section];
    NSMutableArray *arr = [self.childDic objectForKey:key];
    Children *child = [[arr objectAtIndex:sourceIndexPath.row] retain];
    [arr removeObjectAtIndex:sourceIndexPath.row];
    
    NSString *desKey = [self.childDic.allKeys objectAtIndex:destinationIndexPath.section];
    NSMutableArray *desArr = [self.childDic objectForKey:desKey];
    //    [desArr addObject:child];
    [desArr insertObject:child atIndex:destinationIndexPath.row];
    [child release];
    
    [self.tableV reloadData];
    
}

// 编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

// 编辑完成
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSString *key = [self.childDic.allKeys objectAtIndex:indexPath.section];
        NSMutableArray *arr = [self.childDic objectForKey:key];
        [arr removeObjectAtIndex:indexPath.row];
        if (arr.count == 0) {
            [self.childDic removeObjectForKey:key];
        }
        
        [self.tableV reloadData];
    }
    
}

// 使可编辑状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.tableV setEditing:editing animated:animated];
}


// 编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
// 移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

// 选中触发
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    
    NSString *key = [self.childDic.allKeys objectAtIndex:indexPath.section];
    NSMutableArray *arr = [self.childDic objectForKey:key];
    detailVC.child = [arr objectAtIndex:indexPath.row];
    
    [self.navigationController pushViewController:detailVC animated:YES];
    [detailVC release];
    
}

// 右侧排序
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.childDic.allKeys;
}

// 区标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [self.childDic.allKeys objectAtIndex:section];
}

// 区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.childDic.allKeys.count;
}
// 行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *key = [self.childDic.allKeys objectAtIndex:section];
    NSMutableArray *arr = [self.childDic objectForKey:key];
    return arr.count;
}

// cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellStr = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellStr];
    }
    NSString *key = [self.childDic.allKeys objectAtIndex:indexPath.section];
    NSMutableArray *arr = [self.childDic objectForKey:key];
    Children *child = [arr objectAtIndex:indexPath.row];
    cell.textLabel.text = [[arr objectAtIndex:indexPath.row] name];
    cell.detailTextLabel.text = [[arr objectAtIndex:indexPath.row] phone];
    cell.imageView.image = [UIImage imageNamed:child.image];
    return cell;
    
    
    
}

- (void)getData
{
    
    NSString *fileName = [[NSBundle mainBundle] pathForResource:@"HW" ofType:@"plist"];
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:fileName];
    
    self.childDic = [NSMutableDictionary dictionary];
    
    for (NSString *key in dic) {
        
        NSMutableArray *arr = [NSMutableArray array];
        for (NSMutableDictionary *tempDic in [dic objectForKey:key]) {
            
            Children *child = [[Children alloc] init];
            child.name = [tempDic objectForKey:@"name"];
            child.phone = [tempDic objectForKey:@"phone"];
            child.sex = [tempDic objectForKey:@"sex"];
            child.age = [tempDic objectForKey:@"age"];
            child.image = [tempDic objectForKey:@"image"];
            [arr addObject:child];
            [child release];
            
        }
        [self.childDic setObject:arr forKey:key];
    }
    
    
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
